package com.cw.jeeyt.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * 错误页面配置
 *
 * @author: chenw
 * @create: 2018-12-03 21:22
 */
@Configuration
public class ErrorPageConfig {
    /**
     * SpringBoot2.0以上版本WebServerFactoryCustomizer代替之前版本的EmbeddedWebServerFactoryCustomizerAutoConfiguration
     *
     * @return
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        //第一种：java7 常规写法
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                ErrorPage[] errorPages = new ErrorPage[5];
                errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/error/error404");
                errorPages[1] = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/error400");
                errorPages[2] = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/error401");
                errorPages[3] = new ErrorPage(HttpStatus.FORBIDDEN, "/error/error403");
                errorPages[4] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/error500");
                factory.addErrorPages(errorPages);
            }
        };
        //第二种写法：java8 lambda写法
        /*return (factory -> {
            ErrorPage [] errorPages = new ErrorPage[3];
            errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html");
            errorPages[1] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"error/500.html");
            factory.addErrorPages(errorPages);
        });*/
    }

}
